import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static char[] temp = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    public static int len;
    public static List<Integer> list;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        while (t > 0) {
            t--;
            String str = sc.next();
            char[] ch = str.toCharArray();
            len = str.length();
            list = new ArrayList<>();
            dfs(ch, 0, 0);
            int ret = Integer.MAX_VALUE;
            for (int num : list) {
                ret = Math.min(ret, num);
            }
            System.out.println(ret == Integer.MAX_VALUE ? -1 : ret);
        }

        sc.close();
    }

    /**
     * n 为 arr 对应下标， index 为 temp 对应下标
     * @param ch
     * @param n
     */
    public static void dfs(char[] ch, int n, int index) {
        if (n == len) {
            String str = String.valueOf(ch);
            int num = Integer.parseInt(str);
            if (isPrime(num)) {
                list.add(num);
            }
            return;
        }

        if (index == 10) {
            return;
        }

        if (ch[n] == '*') {
            for (int i = 0; i < 10; i++) {
                ch[n] = temp[i];
//                if (n == len - 1) {
//                    String str = String.valueOf(ch);
//                    int num = Integer.parseInt(str);
//                    if (isPrime(num)) {
//                        list.add(num);
//                        return;
//                    }
//                }
                dfs(ch, n + 1, 0);
            }
        } else {
            dfs(ch, n + 1, 0);
        }
    }

    public static boolean isPrime(int num) {
        if (num == 1) {
            return false;
        }
        if (num == 2) {
            return true;
        }
        if ((num % 10) % 2 == 0) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

}
